extends Node2D

class_name BigMap

#region UI控件
@onready var city_info_margin_container: MarginContainer = $CanvasLayer/CityInfoMarginContainer
@onready var button_close: Button = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer3/ButtonClose
@onready var city_name_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/HBoxContainer/CityNameLabel
@onready var city_hero_count_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer/MarginContainer2/HBoxContainer/CityHeroCountLabel
@onready var city_soldiers_count_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer/MarginContainer3/HBoxContainer/CitySoldiersCountLabel
@onready var city_population_value_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer/MarginContainer4/HBoxContainer/CityPopulationValueLabel
@onready var faction_king_name_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer/MarginContainer2/HBoxContainer/FactionKingNameLabel
@onready var city_holder_name_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer/MarginContainer3/HBoxContainer/CityHolderNameLabel
@onready var city_business_value_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer2/MarginContainer2/HBoxContainer/CityBusinessValueLabel
@onready var city_farm_value_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer2/MarginContainer3/HBoxContainer/CityFarmValueLabel
@onready var city_safety_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer3/MarginContainer2/HBoxContainer/CitySafetyLabel
@onready var city_happiness_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer3/MarginContainer3/HBoxContainer/CityHappinessLabel
@onready var city_gold_value_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer4/MarginContainer2/HBoxContainer/CityGoldValueLabel
@onready var city_food_value_label: Label = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/HBoxContainer2/VBoxContainer4/MarginContainer3/HBoxContainer/CityFoodValueLabel
@onready var city_hero_list: VBoxContainer = $CanvasLayer/CityInfoMarginContainer/MarginContainer/VBoxContainer/MarginContainer/ScrollContainer/CityHeroList
#endregion

const city_item_prefab:PackedScene = preload("res://Prefebs/Components/BigMap/city.tscn")
const hero_list_item_prefab:PackedScene = preload("res://Prefebs/Components/BigMap/city_info_hero_list_item.tscn")

var _big_map_root:Node2D

func _ready():
	_big_map_root = get_node("BigMapRoot")
	button_close.pressed.connect(_on_close_button_clicked)
	# 动态生成每个城市
	_generate_city_item()

	pass
	
func _generate_city_item():
	var factionss = GlobalConfig.faction_list
	for f:FactionEntity in factionss:
		for c:CityEntity in f.city_list:
			var city_instance:BigMapCity = city_item_prefab.instantiate()
			_big_map_root.add_child(city_instance)
			city_instance.position = c.city_position
			city_instance.city_clicked.connect(_city_item_clicked)
			city_instance.set("city_entity",c)
			pass
		pass
	pass	



func _city_item_clicked(city:BigMapCity):
	GlobalConfig.enabal_ui = true
	# 修改ui上的显示
	var cn = city.city_entity
	city_name_label.text = cn.city_name
	city_hero_count_label.text = var_to_str(cn.hero_count)
	city_soldiers_count_label.text = var_to_str(cn.total_soliders)
	city_population_value_label.text = var_to_str(cn.population_value)
	var king:HeroEntity = cn.get_king()		
	faction_king_name_label.text = "" if king == null else king.hero_name
	var holder = cn.get_standholder()
	city_holder_name_label.text = "" if holder == null else holder.hero_name
	city_business_value_label.text = var_to_str(cn.business_value)
	city_farm_value_label.text = var_to_str(cn.farm_value)
	city_safety_label.text = var_to_str(cn.city_id)
	city_happiness_label.text = var_to_str(cn.people_happiness_value)
	city_gold_value_label.text = var_to_str(cn.gold_value)
	city_food_value_label.text = var_to_str(cn.food_value)
	
	# 武将列表
	var h_list = city_hero_list.get_children()
	for h_item in h_list:
		city_hero_list.remove_child(h_item)
		pass		
	if cn.hero_count != 0:
		for h in cn.hero_list:
			var item_instance = hero_list_item_prefab.instantiate()
			city_hero_list.add_child(item_instance)
			item_instance.set("hero_entity",h)
			pass
		pass
	
	city_info_margin_container.show()
	pass

func  _on_close_button_clicked():
	GlobalConfig.enabal_ui = false
	city_info_margin_container.hide()
	pass
